﻿using Models;
using System;
using System.Collections.Generic;
using System.Text;

namespace RedisHelper.LogIn
{
    public class UserLogIn
    {
        /// <summary>
        /// 插入Token
        /// </summary>
        /// <param name="token">令牌</param>
        /// <param name="sessionId">私钥</param>
        /// <returns>成功返回true</returns>
        public bool InsertToken(string token, string sessionId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(1).StringSet(token, sessionId, TimeSpan.FromDays(1));
            }
        }

        /// <summary>
        /// 查询Token是否存在
        /// </summary>
        /// <param name="token">令牌</param>
        /// <returns>存在返回私钥, 否则返回null</returns>
        public string IsTokenExist(string token)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var sessionId = rh.GetDatabase(1).StringGet(token);
                if (sessionId.IsNull)
                    return null;
                else
                    return sessionId;
            }
        }
    }
}
